package com.atmc.service.impl;

import com.atmc.entity.ResponseResult;
import com.atmc.mapper.ArtLikerMapper;
import com.atmc.service.AcomentNumService;
import com.atmc.service.ArtLikerService;
import com.atmc.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: SpringCloud1216
 * @BelongsPackage: com.atmc.service.impl
 * @Author: chuyings
 * @CreateTime: 2022-11-15  13:22
 * @Version: 1.0
 */
@Service
public class ArtLikerServiceImpl implements ArtLikerService {
    @Autowired
    ArtLikerMapper artLikerMapper;
    @Autowired
    AcomentNumService acomentNumService;
    @Autowired
    ArticleService articleService;
    @Override
    public Long queryByIdAndName(Long articleId, String userName) {
        return artLikerMapper.queryByIdAndName(articleId,userName);
    }

    @Override
    public int insertOne(Long articleId,String userName) {
        return artLikerMapper.insertOne(articleId,userName);
    }

    @Override
    public int updateOneCre(Long articleId, String userName) {
        return artLikerMapper.updateOneCre(articleId,userName);
    }

    @Override
    public int updateOneDes(Long articleId, String userName) {
        return artLikerMapper.updateOneDes(articleId,userName);
    }

    @Override
    public ResponseResult requestLiker(Long id, String userName) {
        Long statusCode = artLikerMapper.queryByIdAndName(id, userName);
        if (statusCode==null){
            artLikerMapper.insertOne(id,userName);
            artLikerMapper.updateOneCre(id,userName);
            articleService.updateOneLikerCre(id);
        }else if(statusCode==1) {
//            artLikerMapper.updateOneDes(id, userName);
            articleService.updateOneLikerDes(id);
            artLikerMapper.deleteOneByIdAndName(id, userName);
        }


        return new ResponseResult(200,"点赞成功");
    }

    @Override
    public List<Long> selectIdByName(String userName) {
        return artLikerMapper.selectIdByName(userName);
    }

    @Override
    public int deleteOneByIdAndName(Long articleId, String userName) {
        return artLikerMapper.deleteOneByIdAndName(articleId,userName);
    }
}
